﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using System.Data;


namespace Web_cuiki
{
 
    public partial class WebForm3 : System.Web.UI.Page
    {
        public string Catalog;
        public string detail;
        public string moreDetail;
        public SAN_PHAM sp;
        public string cartView;
        protected void Page_Load(object sender, EventArgs e)
        {
            String maSach = Request.QueryString["masach"];
            sp = SanPhamDAO.LaySanPhamTheoMa(maSach);
            cartView = "";
            if (Session["dsChiTiet"] != null)
            {
                string strds = (string)Session["dsChiTiet"];
                List<ChiTietDonDatHangDTO> dsChiTiet = stringToListChiTiet(strds);
                cartView += " <div class='home_cart_content'>"
              + dsChiTiet.Count + " items"
              + "</div> <a href='Cart.aspx' class='view_cart'>view cart</a>";
            }

            Catalog = "";
            List<THE_LOAI> DSTL = TheLoaiDAO.LayDSTheLoai();
            for (int i = 0; i < DSTL.Count; i++)
            {
                Catalog += "<li> <a href='Default.aspx?TheLoai=" + DSTL[i].MaTheLoai + "'>";
                Catalog += DSTL[i].TenTheLoai + "</a></li> ";
            }


            this.detail = "";
            this.moreDetail = "";

            if (sp != null)
            {
                detail += "<div class='feat_prod_box_details'>"
                            + "<div class='prod_img'>"
                            + " <a href='#'>"
                            + "<img src='images/prod1.gif' alt='' title='' border='0' /></a>"
                            + "<br /><br />"
                            + "<a href='images/big_pic.jpg' rel='lightbox'>"
                            + "<img src='images/zoom.gif' alt='' title='' border='0' /></a>"
                            + "</div>"

                            + "<div class='prod_det_box'>"
                                + "<div class='box_top'></div>"
                                + "<div class='box_center'>"
                                    + "<div class='prod_title'>Details</div>"
                                    + "<p class='details'>" + sp.MoTa + " </p>"
                                    + "<div class='price'>"
                                    + "<strong>PRICE:</strong> <span class='red'>" + sp.Gia + " VND</span></div>"

                                    + "<div class='price'>"
                                    + "<strong>COLORS:</strong> <span class='colors'>"
                                    + "<img src='images/color1.gif' alt='' title='' border='0' /></span> <span class='colors'>"
                                    + "<img src='images/color2.gif' alt='' title='' border='0' /></span> <span class='colors'>"
                                    + "<img src='images/color3.gif' alt='' title='' border='0' /></span>"
                                    + "</div> "
                                    + "<div class='clear'> </div> "
                                + "</div>"
                                + "<div class='box_bottom'>"
                                + "</div>"
                            + "</div>"
                            + "<div class='clear'></div>"
                        + "</div>";


                this.detail += "<div id='demo' class='demolayout'>"
                 + "<ul id='demo-nav' class='demolayout'>"
                 + "<li><a class='active' href='#tab1'>More details</a></li>"
                 + "<li><a class='' href='#tab2'>Related books</a></li>"
                 + "</ul>"
                 + "<div class='tabs-container'>"
                 + "<div style='display: block;' class='tab' id='tab1'>"

                 + "<ul class='list'>";
                if (sp.TAC_GIA != null)
                    detail += "<li>Tác giả:" + sp.TAC_GIA.TenTacGia + "</li>";
                if (sp.THE_LOAI != null)
                    detail += "<li>Thể loại: " + sp.THE_LOAI.TenTheLoai + "</li>";
                if (sp.NgayPhatHanh != null)
                    detail += "<li>Ngày phát hành: " + sp.NgayPhatHanh.Value.Date.ToShortDateString() + "</li>";
                if (sp.SoLuongTon != null)
                    detail += "<li>Số lượng tồn: " + sp.SoLuongTon + "</li>";

                detail += "</ul>"

                + "</div>"
                + "<div style='display: none;' class='tab' id='tab2'>"
                + "<div class='new_prod_box'>"
                + "<a href='#'>" + sp.TenSanPham + "</a>"
                + "<div class='new_prod_bg'>"
                + "<a href='#'>"
                + "<img src='images/thumb1.gif' alt='' title='' class='thumb' border='0' /></a>"
                + "</div>"
                + "</div>"
                + "<div class='new_prod_box'>"
                + "<a href='#'>product name</a>"
                + "<div class='new_prod_bg'>"
                + "<a href='#'>"
                + "<img src='images/thumb2.gif' alt='' title='' class='thumb' border='0' /></a>"
                + "</div>"
                + "</div>"
                + "<div class='new_prod_box'>"
                + "<a href='#'>product name</a>"
                + "<div class='new_prod_bg'>"
                + "<a href='#'>"
                + "<img src='images/thumb3.gif' alt='' title='' class='thumb' border='0' /></a>"
                + "</div>"
                + "</div>"
                + "<div class='new_prod_box'>"
                + "<a href='#'>product name</a>"
                + "<div class='new_prod_bg'>"
                + "<a href='#'>"
                + "<img src='images/thumb1.gif' alt='' title='' class='thumb' border='0' /></a>"
                + "</div>"
                + "</div>"
                + "<div class='new_prod_box'>"
                + "<a href='#'>product name</a>"
                + "<div class='new_prod_bg'>"
                + "<a href='#'>"
                + "<img src='images/thumb2.gif' alt='' title='' class='thumb' border='0' /></a>"
                + "</div>"
                + "</div>"
                + "<div class='new_prod_box'>"
                + "<a href='#'>" + sp.TenSanPham + "</a>"
                + "<div class='new_prod_bg'>"
                + "<a href='#'>"
                + "<img src='images/thumb3.gif' alt='' title='' class='thumb' border='0' /></a>"
                + "</div>"
                + "</div>"
                + "<div class='clear'>"
                + "</div>"
                + "</div>"
                + "</div>"
                + "</div>"
                + "<div class='clear'>"
                + "</div>";

            }
            else
            {
                detail += "<h1>Sản phẩm không tồn tại</h1>";
            }

        }

        private List<ChiTietDonDatHangDTO> stringToListChiTiet(string str)
        {
            List<ChiTietDonDatHangDTO> rs = new List<ChiTietDonDatHangDTO>();
            str = WSRequest.EditToBasicFormat(str);
            WSRequest.XMLToObject<List<ChiTietDonDatHangDTO>>(ref rs, str);
            return rs;
        }
        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            List<ChiTietDonDatHangDTO> dsChiTiet = null;
            string sl = tbSoLuong.Text.Trim();

            if (sl != null && sl != "")
            {
                
                if (Session["dsChiTiet"] != null)
                {
                    string strds = (string)Session["dsChiTiet"];
                    dsChiTiet = stringToListChiTiet(strds); 
                }
                else
                {
                    dsChiTiet = new List<ChiTietDonDatHangDTO>();
                }
                if (dsChiTiet != null)
                {                    
                    int index = -1;
                    for (int i = 0; i < dsChiTiet.Count; i++)
                    {
                        if (dsChiTiet[i].MaTaiLieu == sp.MaSanPham)
                            index = i;
                    }
                    if (index >= 0)
                    {
                        dsChiTiet[index].SoLuong += Int32.Parse(sl);
                    }
                    else
                    {
                        ChiTietDonDatHangDTO ct = new ChiTietDonDatHangDTO();
                        ct.DonGia = sp.Gia.Value;
                        ct.SoLuong = Int32.Parse(sl);
                        ct.MaTaiLieu = sp.MaSanPham;
                        
                        dsChiTiet.Add(ct);
                    }
                }

                
                //XmlSerializer serializer = new XmlSerializer(dsChiTiet.GetType());
                //System.IO.MemoryStream stream = new System.IO.MemoryStream();

                //serializer.Serialize(stream,dsChiTiet);

              string str =  WSRequest.ObjectToXML(dsChiTiet.ToArray<ChiTietDonDatHangDTO>());


              Session["dsChiTiet"] = str;
                Response.Redirect("/Detail.aspx?masach=" + sp.MaSanPham);
            }
        }

       
    }
}